#!/bin/bash

source  ../lib/gitlib.sh
source  ../lib/lib.sh

git_update "http://dgggit09-rd.huawei.com/CRDU_EULEROS_GIT/open_source/kernelspace/kernel-4.1-euleros" $GERRIT_BRANCH
pushd $git_root/kernel-4.1-euleros
curl -kLo `git rev-parse --git-dir`/hooks/commit-msg http://peulerosci@dgggit09-rd.huawei.com/tools/hooks/commit-msg
chmod +x `git rev-parse --git-dir`/hooks/commit-msg
popd


pushd $git_root/kernel-4.1-euleros
last10_id=`git log --oneline -n10 | awk '{print $3}'`
if echo $last10_id | grep $GERRIT_PATCHSET_REVISION; then
    echo "no need to retrigger. already update kernel-4.1-euleros"
    exit 0
fi

#spec文件必须包含%define hwel_release字段
#hwel_release字段的值必须包含h h后的数字为release递增字段
for fspec in `ls *.spec`
do
    if grep '%define hwel_release' $fspec &>/dev/null; then
        release=`cat $fspec | grep '%define hwel_release' | awk '{print $3}'`
        if ! echo $release | grep 'h' &>/dev/null; then
            echo "Error: can not find delimit h in $fspec hwel_release"
            exit 1
        fi
        increase_num=`echo $release | awk -F'h' '{print $2}'`
        let new_increase_num=$increase_num+1
        new_release=`echo $release | sed "s#h${increase_num}#h${new_increase_num}#g"`
        sed -i "/define hwel_release/s/$release/$new_release/g" $fspec
    else
        echo "Error: can not find hwel_release in $fspec"
        exit 1
    fi
done
git_commit "kernel-4.1: $GERRIT_PATCHSET_REVISION
$GERRIT_CHANGE_SUBJECT
$GERRIT_CHANGE_OWNER_NAME"
git_push $GERRIT_BRANCH
